package com.atmilan.quartz.service.impl;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import org.apache.ibatis.binding.MapperMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.StoreProductAttrValueMapper;
import com.atmilan.quartz.model.entity.crmeb.StoreProductAttrValue;
import com.atmilan.quartz.service.IStoreProductAttrValueService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collection;
import java.util.function.Function;

/**
* @author Mr.Zhang edit by stivepeim 7-6
* @description StoreProductAttrValueServiceImpl 接口实现
* @date 2020-05-27
*/
@DS("crmeb")
@Service
public class StoreProductAttrValueServiceImpl extends ServiceImpl<StoreProductAttrValueMapper, StoreProductAttrValue> implements IStoreProductAttrValueService {
    @Autowired
    StoreProductAttrValueMapper storeProductAttrValueMapper;

    @Override
    public void deleteAll() {
        storeProductAttrValueMapper.deleteAll();
    }

    /**
     * 根据商品id删除AttrValue
     * @param productId 商品id
     * @reture 删除结果
     */
    @Override
    public boolean removeByProductId(Integer productId) {
        LambdaQueryWrapper<StoreProductAttrValue> lambdaQW = new LambdaQueryWrapper<>();
        lambdaQW.eq(StoreProductAttrValue::getProductId, productId);
        return storeProductAttrValueMapper.delete(lambdaQW) > 0;
    }

    @Override
    public boolean updateBatchByQueryWrapper(Collection<StoreProductAttrValue> entityList, Function<StoreProductAttrValue, LambdaQueryWrapper> queryWrapperFunction) {
        String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE);
        return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
            MapperMethod.ParamMap param = new MapperMethod.ParamMap();
            param.put(com.baomidou.mybatisplus.core.toolkit.Constants.ENTITY, entity);
            param.put(com.baomidou.mybatisplus.core.toolkit.Constants.WRAPPER, queryWrapperFunction.apply(entity));
            sqlSession.update(sqlStatement, param);
        });
    }

}
